;*****************************************************************************************************
;+
; NAME:
;
;      ePLOT (enhanced PLOT)
;
; PURPOSE:
;
;      This is the enhanced version of the standard IDL PLOT
;      command. It uses the same syntax and support most of the IDL
;      plotting keywords. The output plot is displayed in an IGL window
;      so that you can easily interact with it.
;
;      NOTE: The plot always has exact axis ranges. Extended ranges as
;            calculated by IDL would prevent the pan and zoom interactions to
;            work correctly. In most cases, having exact ranges is what
;            is desired anyway.
;
; CALLING SEQUENCE:
;
;      ePLOT, x, y
;
; ARGUMENTS:
;
;     X:            The independent variable used to produce the
;                   plot. If not provided, set to FINDGEN(N_Elements(Y)).
;
;     Y:            The dependent variable used to produce the plot.
;
;                 
; KEYWORD PARAMETERS:
;
;     AXISCOLOR:    The name of the color for the X and Y axis. The
;                   default is !IGL_P.Axiscolor
;
;     BACKGROUND:   The name of the background color. The default is
;                   !IGL_P.background
;
;     CHARSIZE:     The character size for the annotations (float). The default
;                   is !IGL_P.charsize
;
;     CHARTHICK:    The character thickness for the annotations (integer). The
;                   default is !IG_P.charthick
;
;     COLOR:        The name of the foreground color for the plot. The
;                   default is !IGL_P.color
;
;     FONT:         The character font for the annotations(integer).
;                   -1 : Herschey, 0 : Hardware, 1 : True Type. The
;                   default is !IGL_P.font
;
;     LINESTYLE:    The style for the plot line (integer). The default
;                   is  !IGL_P.linestyle
;
;     NOCLIP:       If set, prevent the clipping at the edge of the
;                   plot window. The default is !IGL_P.noclip
;
;
;     NODATA:       Set this keyword to plot only the axis and the
;                   annotations.
;
;     NODRAW:       *IGL ADDITION* : Set this keyword to prevent the
;                   current window to be refreshed. The plot and axis
;                   are added to the window but they will not be
;                   visible until the window is refreshed (by calling
;                   another IGL plotting command for example). This is
;                   useful is you wish to issue several plotting
;                   commands but want to display the results only at
;                   the end. 
;
;     NOERASE:      If set, prevent the current window to be erased
;                   before drawing the plot. The default is
;                   !IGL_P.noerase
;
;     POLAR:        Set this keyword to produce a polar plot. Contrary
;                   to the standard IDL PLOT command, a polar plot is
;                   automatically produced with a single X and Y axis
;                   passing at (0,0).
;
;     POSITION:     A 4-element vector indicating the position of the
;                   plot in the window in normalized coordinates. The
;                   default is !IGL_P.position
;
;     PSYM:         An integer specifiying the plotting symbol to
;                   use. All the symbols from the SYMCAT
;                   routine (Coyote Library) are directly available. The
;                   default is !IGL_P.psym
;
;     STATIC:       *IGL ADDITION* : Set this keyword to indicate that
;                   the data coordinate system associated with the
;                   plot is static. This prevents the data X and Y
;                   ranges to be changed and as a consequence, the
;                   plot cannot be panned or zoomed. Note that you can
;                   interactively change this behaviour after the
;                   plot is produced.
;
;     SYMSIZE:      The size of the symbols when PSYM is set
;                   (float). The default is !IGL_P.symsize
;
;     THICK:        The thickness of the line (integer). The default
;                   is !IGL_P.thick
;
;     TICKLEN:      Specify the length of the axis tick marks, in
;                   units of the fraction of the plot window. The
;                   default is !IGL_P.ticklen.
;
;     TITLE:        The text string used as the plot title. THe
;                   default is !IGL_P.title
;
;     [XY]CHARSIZE: The character size for the axis annotations. Be
;                   careful that this is like the regular IDL
;                   keyword meaning that it only indicates a multiplying
;                   factor to the global character size specified by
;                   the CHARSIZE keyword. The default is !IGL_[XY].charsize
;
;     [XY]GRIDSTYLE: Integer specifying the linestyle to use for the
;                    axis grid. The default is !IGL_[XY].gridstyle
;
;     [XY]LOG:      Set this keyword if you want logarithmic axis
;                   scaling.
;
;     [XY]MINOR:    The number of minor axis tick intervals. The default
;                   is !IGL_[XY].minor
;
;     [XY]RANGE:    2-element vector specifying the axis data
;                   range. The default is !IGL_[XY].range
;
;     [XY]STYLE:    Integer specifying the axis style options. Note
;                   that the "extended range" option ([XY]style=2) is
;                   disabled because it prevents pan and zoom
;                   interactions to work correctly. The default is !IGL_[XY].style
;
;     [XY]THICK:    The thickness of the line forming the axis. The
;                   default is !IGL_[XY].thick
;
;     [XY]TICKFORMAT: String specifying the format used for the axis
;                     labels. The default is !IGL_[XY].tickformat
;
;     [XY]TICKINTERVAL: Scalar indicating the interval between major
;                       tick marks. The default is !IGL_[XY].tickinterval
;
;     [XY]TICKLAYOUT: Integer specifying the axis layout. Possible
;                     values are : 0 = Normal layout, 1 = Only labels,
;                     2 = Box layout. The default is !IGL_[XY].ticklayout
;
;     [XY]TICKLEN:  The length of the axis tick marks. This overrides
;                   the global setting specified by the TICKLEN
;                   keyword. The default is !IGL_[XY].ticklen
;
;     [XY]TICKNAME: String array specifying the annotation of each
;                   tick marks. The default is !IGL_[XY].tickname
;
;     [XY]TICKS:    Integer specifying the number of major tick
;                   intervals. The default is !IGL_[XY].ticks
;
;     [XY]TICKUNITS: Vector of strings indicating the units used for
;                    axis tick labeling. The default is !IGL_[XY].tickunits
;
;     [XY]TICKV:    Array of data values for each tick mark. The
;                   default is !IGL_[XY].tickv
;
;     [XY]TITLE:    The text string used as the axis title. The
;                   default is !IGL_[XY].title
;    
; PACKAGE:
;
;    Interactive Graphics Library (IGL)
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
;
; MODIFICATION HISTORY:
;
;     Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
PRO ePLOT, x, y, $
           AXISCOLOR=axiscolor, $
           BACKGROUND=background, $
           CHARSIZE=charsize, $
           CHARTHICK=charthick, $
           COLOR=color, $
           FONT=font, $
           LINESTYLE=linestyle, $
           NOCLIP=noclip, $
           NODATA=nodata, $
           NODRAW=nodraw, $
           NOERASE=noerase, $
           POLAR=polar, $
           POSITION=position, $
           PSYM=psym, $
           STATIC=static, $
           SYMSIZE=symsize, $
           THICK=thick, $
           TICKLEN=ticklen, $
           TITLE=title, $
           XCHARSIZE=xcharsize, $
           XGRIDSTYLE=xgridstyle, $
           XLOG=xlog, $
           XMINOR=xminor, $
           XRANGE=xrange, $
           XSTYLE=xstyle, $
           XTHICK=xthick, $
           XTICKFORMAT=xtickformat, $
           XTICKINTERVAL=xtickinterval, $
           XTICKLAYOUT=xticklayout, $
           XTICKLEN=xticklen, $
           XTICKNAME=xtickname, $
           XTICKS=xticks, $
           XTICKUNITS=xtickunits, $
           XTICKV=xtickv, $
           XTITLE=xtitle, $
           YCHARSIZE=ycharsize, $
           YGRIDSTYLE=ygridstyle, $
           YLOG=ylog, $
           YMINOR=yminor, $
           YRANGE=yrange, $
           YSTYLE=ystyle, $
           YTHICK=ythick, $
           YTICKFORMAT=ytickformat, $
           YTICKINTERVAL=ytickinterval, $
           YTICKLAYOUT=yticklayout, $
           YTICKLEN=yticklen, $
           YTICKNAME=ytickname, $
           YTICKS=yticks, $
           YTICKUNITS=ytickunits, $
           YTICKV=ytickv, $
           YTITLE=ytitle


  COMPILE_OPT idl2, HIDDEN
  ON_ERROR, 2

  ;; Initialize the global system variables
  IGL_InitSysVars

  ;; Check positional arguments
  CASE N_PARAMS() OF
     
     0: Message, 'No arguments passed to ePLOT'
     
     1: BEGIN
        ydata = x
        xdata = Findgen(N_Elements(ydata))
     END
     
     2: BEGIN
        xdata = x
        ydata = y
     END

     ELSE:
     
  ENDCASE

  ;; Make sure both arguments have same number of elements
  IF N_Elements(xdata) NE N_Elements(ydata) THEN Message, 'Input arguments must have same number of elements'

  ;; Default keyword values

  ;; General keywords
  IF N_Elements(axiscolor) EQ 0 THEN axiscolor = !IGL_P.axiscolor
  IF N_Elements(background) EQ 0 THEN background = !IGL_P.background
  IF N_Elements(charsize) EQ 0 THEN charsize = (!IGL_P.charsize LE 0) ? 1 : !IGL_P.charsize
  IF N_Elements(charthick) EQ 0 THEN charthick = (!IGL_P.charthick LE 0) ? 1 : !IGL_P.charthick
  IF N_Elements(color) EQ 0 THEN color = !IGL_P.color
  IF N_Elements(font) EQ 0 THEN font = !IGL_P.font
  IF N_Elements(linestyle) EQ 0 THEN linestyle = !IGL_P.linestyle
  IF N_Elements(noclip) EQ 0 THEN noclip = !IGL_P.noclip
  IF N_Elements(noerase) EQ 0 THEN noerase = !IGL_P.noerase
  IF N_Elements(position) EQ 0 THEN position = !IGL_P.position
  IF N_Elements(psym) EQ 0 THEN psym = !IGL_P.psym
  IF N_Elements(symsize) EQ 0 THEN symsize = (!IGL_P.symsize LE 0) ? 1 : !IGL_P.symsize
  IF N_Elements(thick) EQ 0 THEN thick = (!IGL_P.thick LE 0) ? 1 : !IGL_P.thick
  IF N_Elements(ticklen) EQ 0 THEN ticklen = (!IGL_P.ticklen LE 0) ? 0.02 : !IGL_P.ticklen
  IF N_Elements(title) EQ 0 THEN title = !IGL_P.title

  ;; X axis keywords
  IF N_Elements(xcharsize) EQ 0 THEN xcharsize = (!IGL_X.charsize LE 0) ? 1 : !IGL_X.charsize
  IF N_Elements(xgridstyle) EQ 0 THEN xgridstyle = !IGL_X.gridstyle
  IF N_Elements(xminor) EQ 0 THEN xminor = !IGL_X.minor
  IF N_Elements(xrange) EQ 0 THEN xrange = !IGL_X.range
  IF N_Elements(xstyle) EQ 0 THEN xstyle = !IGL_X.style
  IF N_Elements(xthick) EQ 0 THEN xthick = (!IGL_X.thick LE 0) ? 1 : !IGL_X.thick
  IF N_Elements(xtickformat) EQ 0 THEN xtickformat = !IGL_X.tickformat
  IF N_Elements(xtickinterval) EQ 0 THEN xtickinterval = !IGL_X.tickinterval
  IF N_Elements(xticklayout) EQ 0 THEN xticklayout = !IGL_X.ticklayout
  IF N_Elements(xticklen) EQ 0 THEN xticklen = (!IGL_X.ticklen LE 0) ? ticklen : !IGL_X.ticklen
  IF N_Elements(xtickname) EQ 0 THEN xtickname = !IGL_X.tickname
  IF N_Elements(xticks) EQ 0 THEN xticks = !IGL_X.ticks
  IF N_Elements(xtickunits) EQ 0 THEN xtickunits = !IGL_X.tickunits
  IF N_Elements(xtickv) EQ 0 THEN xtickv = !IGL_X.tickv
  IF N_Elements(xtitle) EQ 0 THEN xtitle = !IGL_X.title

  ;; Y axis keywords
  IF N_Elements(ycharsize) EQ 0 THEN ycharsize = (!IGL_Y.charsize LE 0) ? 1 : !IGL_Y.charsize
  IF N_Elements(ygridstyle) EQ 0 THEN ygridstyle = !IGL_Y.gridstyle
  IF N_Elements(yminor) EQ 0 THEN yminor = !IGL_Y.minor
  IF N_Elements(yrange) EQ 0 THEN yrange = !IGL_Y.range
  IF N_Elements(ystyle) EQ 0 THEN ystyle = !IGL_Y.style
  IF N_Elements(ythick) EQ 0 THEN ythick = (!IGL_Y.thick LE 0) ? 1 : !IGL_Y.thick
  IF N_Elements(ytickformat) EQ 0 THEN ytickformat = !IGL_Y.tickformat
  IF N_Elements(ytickinterval) EQ 0 THEN ytickinterval = !IGL_Y.tickinterval
  IF N_Elements(yticklayout) EQ 0 THEN yticklayout = !IGL_Y.ticklayout
  IF N_Elements(yticklen) EQ 0 THEN yticklen = (!IGL_Y.ticklen LE 0) ? ticklen : !IGL_Y.ticklen
  IF N_Elements(ytickname) EQ 0 THEN ytickname = !IGL_Y.tickname
  IF N_Elements(yticks) EQ 0 THEN yticks = !IGL_Y.ticks
  IF N_Elements(ytickunits) EQ 0 THEN ytickunits = !IGL_Y.tickunits
  IF N_Elements(ytickv) EQ 0 THEN ytickv = !IGL_Y.tickv
  IF N_Elements(ytitle) EQ 0 THEN ytitle = !IGL_Y.title

  ;; Save device settings and switch to window device
  entryDevice = !d.name
  IF (strupcase(!version.os_family) EQ 'UNIX') THEN set_plot, 'X'
  IF (strupcase(!version.os_family) EQ 'WINDOWS') THEN set_plot, 'WIN'

  ;; Get current window, create one if none is available
  currentWindow = IGL_GetCurrentWindow()
  IF OBJ_ISA_VALID(currentWindow,'IGL_WINDOW') EQ 0 THEN currentWindow = obj_new('IGL_Window')
  currentWindow -> GetProperty, Window_Xsize=Xsize, Window_Ysize=Ysize

  ;; Make sure we have a valid position vector
  IF Total(position) LE 0 THEN pos = [0.1,0.1,0.9,0.9] ELSE pos = position
  IF (pos[0] GE pos[2]) OR (pos[1] GE pos[3]) THEN Message, 'Invalid POSITION array'

  ;; Take care of multiplots according to !IGL_P.multi. Basically, we
  ;; need to calculate the position of the plot in the window. We let
  ;; IDL do that for us by drawing a plot in a pixmap and retrieving the plot
  ;; window afterwards.
  
  multi = !IGL_P.multi
  DontErase = noerase
  
  IF (multi[1] GT 0) OR (multi[2] GT 0) THEN BEGIN ; Do we need multiple plots ?
      
     IF (multi[0] GT 0) THEN DontErase = 1 ; Only erase the window if we are the first plot of the grid

     ;; Save the IDL multi property and set it to the IGL value
     currentMulti = !P.multi
     !P.multi = multi

     ;; We create a pixmap with the same size as the current IGL window
     window, /free, /pixmap, xsize=xsize, ysize=ysize     
     wPix = !D.window

     ;; Advance the multi state by drawing a dummy plot
     plot, fltarr(2), xstyle=5, ystyle=5

     ;;  Save the plot position
     pos = [!x.window[0],!y.window[0],!x.window[1],!y.window[1]]

     ;; Save the new multi in the IGL variable and restore the IDL
     ;; value 
     !IGL_P.multi = !P.multi
     !P.multi = currentMulti

     ;; Delete the pixmap
     IF (windowAvailable(wPix)) THEN wdelete, wPix
     
  ENDIF
  
  ;; If we want a polar plot, make sure the radius argument is positive
  IF Keyword_Set(polar) THEN BEGIN     
    indices = where(xdata LT 0,count)
    IF count GT 0 THEN message, 'Polar plots require a positive radius argument'
  ENDIF
  
  ;; Make sure the X and Y data ranges are valid. If not, calculate new
  ;; ones based on the minimum and maximum data value. For polar
  ;; plots, we need to convert to cartesian coordinates to get the
  ;; ranges.

  ;; X range
  IF (xrange[0] EQ xrange[1]) THEN BEGIN
     
     IF keyword_set(polar) THEN BEGIN
        rects = CV_COORD(from_polar=transpose([[ydata],[xdata]]),/to_rect)
        xrange = [min(rects[0,*]),max(rects[0,*])]
     ENDIF ELSE xrange = [min(xdata),max(xdata)]
     
  ENDIF

  ;; Y range
  IF (yrange[0] EQ yrange[1]) THEN BEGIN
     
     IF keyword_set(polar) THEN BEGIN
        rects = CV_COORD(from_polar=transpose([[ydata],[xdata]]),/to_rect)
        yrange = [min(rects[1,*]),max(rects[1,*])]
     ENDIF ELSE yrange = [min(ydata),max(ydata)]
     
  ENDIF
     
  ;; Check the axis style options
  noXAxis = bitget(xstyle,2,/silent)
  noYAxis = bitget(ystyle,2,/silent)
  OnlyOneXaxis = bitget(xstyle,3,/silent)
  OnlyOneYaxis = bitget(ystyle,3,/silent)

  ;; Create a new graphic object to hold the plot and the axis
  graphic = obj_new('IGL_Graphic',POSITION=pos)

  ;; Initialize the coordinate system
  coords = obj_new('IGL_COORD', $
                   POSITION=pos, $
                   XRANGE=xrange, $
                   YRANGE=yrange, $
                   XLOG=Keyword_Set(xlog), $
                   YLOG=Keyword_set(ylog), $
                   STATIC=Keyword_Set(static) $
                   )

  ;; Create the plot
  IF Keyword_Set(nodata) THEN BEGIN
     
     plotObject = obj_new('IGL_Plot', $
                          XDATA=[0], $
                          YDATA=[0], $
                          PSYM=0, $
                          LINESTYLE=0, $
                          COORD_OBJECT=coords $
                         )
  ENDIF ELSE BEGIN
  
     plotObject = obj_new('IGL_Plot', $
                          XDATA=xdata, $
                          YDATA=ydata, $
                          COLOR=color, $
                          LINESTYLE=linestyle, $
                          CLIP=(1-Keyword_set(noclip)), $
                          POLAR=Keyword_set(polar), $
                          PSYM=psym, $
                          SYMSIZE=symsize, $
                          THICK=thick, $
                          COORD_OBJECT=coords $
                         )

  ENDELSE

  ;; Create the plot title if needed
  IF (title NE '') THEN BEGIN

     ;; Calculate the title position
     xloc = 0.5
     yloc = pos[3]+!D.Y_CH_SIZE/float(Ysize)
     yloc = (yloc-pos[1])/(pos[3]-pos[1]) ; Convert to graphic coordinates

     ;; Initialize the text object
     textObject = obj_new('IGL_Text',title, $
                          X=xloc, $
                          Y=yloc, $
                          ALIGNMENT=1, $
                          CHARSIZE=1.25*charsize, $
                          CLIP=0, $
                          COLOR=axiscolor, $
                          FONT=(font+1), $
                          THICKNESS=charthick, $
                          ORIENTATION=0.0 $
                         )

     ;; Get the graphic's coordinate system to assign it to the text object
     graphic -> GetProperty, Coord_Object=graphicCoords

     ;; Set object properties
     textObject -> SetProperty, Name='Title', Coord_Object=graphicCoords

     ;; Add the title to the plot
     plotObject -> Add, textObject
     
  ENDIF

  

  ;; Create the axis
  
  IF Keyword_Set(polar) THEN BEGIN ; Polar plot : needs 2 axis passing at the center of the plot

     ;; Due to a bad clipping when using polar plots + exact axis
     ;; range, we need to use a slightly smaller plot window
     IF Total(position) EQ 0 THEN pos = [0.15,0.15,0.85,0.85] ELSE pos = position
     graphic -> SetProperty, Position=pos

     ;; Calculate the (0,0) data point in normalized coordinates
     xx = (pos[0]*xrange[1]-pos[2]*xrange[0]) / (xrange[1]-xrange[0])
     yy = (pos[1]*yrange[1]-pos[3]*yrange[0]) / (yrange[1]-yrange[0])

     ;; Convert to graphic coordinates
     xgr = (xx-pos[0])/(pos[2]-pos[0])
     ygr = (yy-pos[1])/(pos[3]-pos[1])
     
     IF noXAxis EQ 0 THEN BEGIN ; We need an X axis
        
        xAxis = obj_new('IGL_Axis', $
                        COORD_OBJECT=coords, $
                        XLOC=xgr, $
                        YLOC=ygr, $
                        DIRECTION=0, $
                        TICKDIR=0, $
                        COLOR=axiscolor, $
                        CHARSIZE=(xcharsize*charsize), $
                        CHARTHICK=charthick, $
                        FONT=(font+1), $
                        GRIDSTYLE=xgridstyle, $
                        THICK=xthick, $
                        MINOR=xminor, $
                        TICKFORMAT=xtickformat, $
                        TICKNAME=xtickname, $
                        TICKINTERVAL=xtickinterval, $
                        TICKLAYOUT=xticklayout, $
                        TICKLEN=xticklen, $
                        TICKS=xticks, $
                        TICKUNITS=xtickunits, $
                        TICKV=xtickv $
                        )

        ;; Create the axis title if needed
        IF (xtitle NE '') THEN BEGIN

           ;; Calculate the title position
           xloc = 0.5
           yloc = yy-3.0*charsize*xcharsize*!D.Y_CH_SIZE/float(Ysize)
           yloc = (yloc-pos[1])/(pos[3]-pos[1]) ; Convert to graphic coordinates

           ;; Initialize the text object
           textObject = obj_new('IGL_Text',xtitle, $
                                X=xloc, $
                                Y=yloc, $
                                ALIGNMENT=1, $
                                CHARSIZE=charsize*xcharsize, $
                                CLIP=0, $
                                COLOR=axiscolor, $
                                FONT=(font+1), $
                                THICKNESS=charthick, $
                                ORIENTATION=0.0 $
                               )

           ;; Get the graphic's coordinate system to assign it to the text object
           graphic -> GetProperty, Coord_Object=graphicCoords

           ;; Set object properties
           textObject -> SetProperty, Name='Axis Title', Coord_Object=graphicCoords

           ;; Add the title to the axis
           xAxis -> Add, textObject
     
        ENDIF                        

        ;; Add the axis to the plot
        plotObject -> Add, xAxis
        
     ENDIF

     IF noYAxis EQ 0 THEN BEGIN ; We need a Y axis

        yAxis = obj_new('IGL_Axis', $
                        COORD_OBJECT=coords, $
                        XLOC=xgr, $
                        YLOC=ygr, $
                        DIRECTION=1, $
                        TICKDIR=0, $
                        COLOR=axiscolor, $
                        CHARSIZE=(ycharsize*charsize), $
                        CHARTHICK=charthick, $
                        FONT=(font+1), $
                        GRIDSTYLE=ygridstyle, $
                        THICK=ythick, $
                        MINOR=yminor, $
                        TICKFORMAT=ytickformat, $
                        TICKNAME=ytickname, $
                        TICKINTERVAL=ytickinterval, $
                        TICKLAYOUT=yticklayout, $
                        TICKLEN=yticklen, $
                        TICKS=yticks, $
                        TICKUNITS=ytickunits, $
                        TICKV=ytickv $
                        )

        ;; Create the axis title if needed
        IF (ytitle NE '') THEN BEGIN

           ;; Calculate the title position
           yloc = 0.5
           xloc = xx-2.8*charsize*ycharsize*!D.X_CH_SIZE/float(Xsize)
           xloc = (xloc-pos[0])/(pos[2]-pos[0]) ; Convert to graphic coordinates

           ;; Initialize the text object
           textObject = obj_new('IGL_Text',ytitle, $
                                X=xloc, $
                                Y=yloc, $
                                ALIGNMENT=1, $
                                CHARSIZE=charsize*ycharsize, $
                                CLIP=0, $
                                COLOR=axiscolor, $
                                FONT=(font+1), $
                                THICKNESS=charthick, $
                                ORIENTATION=90.0 $
                               )

           ;; Get the graphic's coordinate system to assign it to the text object
           graphic -> GetProperty, Coord_Object=graphicCoords

           ;; Set object properties
           textObject -> SetProperty, Name='Axis Title', Coord_Object=graphicCoords

           ;; Add the title to the axis
           yAxis -> Add, textObject
     
        ENDIF                        

        ;; Add the axis to the plot
        plotObject -> Add, yAxis
        
     ENDIF
     
  ENDIF ELSE BEGIN              ; Regular plot, need 4 axis

     ;; X AXIS
     IF noXAxis EQ 0 THEN BEGIN ; We need at least the bottom axis

        bottomAxis = obj_new('IGL_Axis', $
                             COORD_OBJECT=coords, $
                             XLOC=0.0, $
                             YLOC=0.0, $
                             DIRECTION=0, $
                             TICKDIR=0, $
                             COLOR=axiscolor, $
                             CHARSIZE=(xcharsize*charsize), $
                             CHARTHICK=charthick, $
                             FONT=(font+1), $
                             GRIDSTYLE=xgridstyle, $
                             THICK=xthick, $
                             MINOR=xminor, $
                             TICKFORMAT=xtickformat, $
                             TICKNAME=xtickname, $
                             TICKINTERVAL=xtickinterval, $
                             TICKLAYOUT=xticklayout, $
                             TICKLEN=xticklen, $
                             TICKS=xticks, $
                             TICKUNITS=xtickunits, $
                             TICKV=xtickv $
                            )

        ;; Create the axis title if needed
        IF (xtitle NE '') THEN BEGIN

           ;; Calculate the title position
           xloc = 0.5
           yloc = pos[1]-3.0*charsize*xcharsize*!D.Y_CH_SIZE/float(Ysize)
           yloc = (yloc-pos[1])/(pos[3]-pos[1]) ; Convert to graphic coordinates
           
           ;; Initialize the text object
           textObject = obj_new('IGL_Text',xtitle, $
                                X=xloc, $
                                Y=yloc, $
                                ALIGNMENT=1, $
                                CHARSIZE=charsize*xcharsize, $
                                CLIP=0, $
                                COLOR=axiscolor, $
                                FONT=(font+1), $
                                THICKNESS=charthick, $
                                ORIENTATION=0.0 $
                               )

           ;; Get the graphic's coordinate system to assign it to the text object
           graphic -> GetProperty, Coord_Object=graphicCoords

           ;; Set object properties
           textObject -> SetProperty, Name='Axis Title', Coord_Object=graphicCoords

           ;; Add the title to the axis
           bottomAxis -> Add, textObject
     
        ENDIF                        

        ;; Add the axis to the plot
        plotObject -> Add, bottomAxis

        IF OnlyOneXaxis EQ 0 THEN BEGIN ; We need the top axis

           topAxis = obj_new('IGL_Axis', $
                             COORD_OBJECT=coords, $
                             XLOC=0.0, $
                             YLOC=1.0, $
                             DIRECTION=0, $
                             TICKDIR=1, $
                             COLOR=axiscolor, $
                             CHARSIZE=(xcharsize*charsize), $
                             CHARTHICK=charthick, $
                             FONT=(font+1), $
                             GRIDSTYLE=xgridstyle, $
                             THICK=xthick, $
                             MINOR=xminor, $
                             TICKFORMAT='(A1)', $
                             TICKNAME=xtickname, $
                             TICKINTERVAL=xtickinterval, $
                             TICKLAYOUT=xticklayout, $
                             TICKLEN=xticklen, $
                             TICKS=xticks, $
                             TICKUNITS=xtickunits, $
                             TICKV=xtickv $
                            )

           ;; Add the axis to the plot
           plotObject -> Add, topAxis
           
        ENDIF

     ENDIF

     ;; Y AXIS
     IF noYAxis EQ 0 THEN BEGIN ; We need at least the left axis

        leftAxis = obj_new('IGL_Axis', $
                           COORD_OBJECT=coords, $
                           XLOC=0.0, $
                           YLOC=0.0, $
                           DIRECTION=1, $
                           TICKDIR=0, $
                           COLOR=axiscolor, $
                           CHARSIZE=(ycharsize*charsize), $
                           CHARTHICK=charthick, $
                           FONT=(font+1), $
                           GRIDSTYLE=ygridstyle, $
                           THICK=ythick, $
                           MINOR=yminor, $
                           TICKFORMAT=ytickformat, $
                           TICKNAME=ytickname, $
                           TICKINTERVAL=ytickinterval, $
                           TICKLAYOUT=yticklayout, $
                           TICKLEN=yticklen, $
                           TICKS=yticks, $
                           TICKUNITS=ytickunits, $
                           TICKV=ytickv $
                          )

        ;; Create the axis title if needed
        IF (ytitle NE '') THEN BEGIN

           ;; Calculate the title position
           yloc = 0.5
           middle = 0.5*(yrange[1]+yrange[0])
           IF middle NE 0 THEN ndigits = Floor(alog10(Abs(middle))+1) ELSE ndigits = 1
           xloc = pos[0]-(ndigits+2.5)*charsize*ycharsize*!D.X_CH_SIZE/float(Xsize)
           xloc = (xloc-pos[0])/(pos[2]-pos[0]) ; Convert to graphic coordinates
           
           ;; Initialize the text object
           textObject = obj_new('IGL_Text',ytitle, $
                                X=xloc, $
                                Y=yloc, $
                                ALIGNMENT=1, $
                                CHARSIZE=charsize*ycharsize, $
                                CLIP=0, $
                                COLOR=axiscolor, $
                                FONT=(font+1), $
                                THICKNESS=charthick, $
                                ORIENTATION=90.0 $
                               )

           ;; Get the graphic's coordinate system to assign it to the text object
           graphic -> GetProperty, Coord_Object=graphicCoords

           ;; Set object properties
           textObject -> SetProperty, Name='Axis Title', Coord_Object=graphicCoords

           ;; Add the title to the axis
           leftAxis -> Add, textObject
     
        ENDIF                        

        ;; Add the axis to the plot
        plotObject -> Add, leftAxis

        IF OnlyOneYaxis EQ 0 THEN BEGIN ; We need the right axis

           rightAxis = obj_new('IGL_Axis', $
                               COORD_OBJECT=coords, $
                               XLOC=1.0, $
                               YLOC=0.0, $
                               DIRECTION=1, $
                               TICKDIR=1, $
                               COLOR=axiscolor, $
                               CHARSIZE=(ycharsize*charsize), $
                               CHARTHICK=charthick, $
                               FONT=(font+1), $
                               GRIDSTYLE=ygridstyle, $
                               THICK=ythick, $
                               MINOR=yminor, $
                               TICKFORMAT='(A1)', $
                               TICKNAME=ytickname, $
                               TICKINTERVAL=ytickinterval, $
                               TICKLAYOUT=yticklayout, $
                               TICKLEN=yticklen, $
                               TICKS=yticks, $
                               TICKUNITS=xtickunits, $
                               TICKV=ytickv $
                              )

           ;; Add the axis to the plot
           plotObject -> Add, rightAxis
           
        ENDIF

     ENDIF

  ENDELSE

  ;; Update the coordinate system
  IGL_SetSysVars, 'currentCoord', coords

  ;; Add the plot to the graphic
  graphic -> Add, plotObject

  ;; Erase the window
  IF Keyword_Set(DontErase) EQ 0 THEN BEGIN
     currentWindow -> SetProperty, Window_Color=Background
     currentWindow -> ClearWindow, /NoRefresh
  ENDIF

  ;; Add the graphic to the window
  currentWindow -> AddGraphic, graphic

  ;; Refresh the property editor
  currentWindow -> GetProperty, PropertyPanel=PropertyPanel
  IF OBJ_VALID(PropertyPanel) THEN PropertyPanel -> UpdateTreeBrowser
  
  ;; Draw window's content
  IF Keyword_set(nodraw) EQ 0 THEN currentWindow -> DrawWindow, /Buffered_Output

  ;; Restore device
  Set_Plot, entryDevice

END
